package com.lishuang.pet.controller;

import com.lishuang.basic.exception.BusinessException;
import com.lishuang.basic.util.AjaxResult;
import com.lishuang.basic.util.PageList;
import com.lishuang.pet.domain.SearchMasterMsg;
import com.lishuang.pet.dto.MsgDto;
import com.lishuang.pet.query.SearchMasterMsgQuery;
import com.lishuang.pet.service.ISearchMasterMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/searchMasterMsg")
@Api(value = "寻主消息管理API", description = "寻主消息管理相关的CRUD功能")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     *
     * @param searchMasterMsg 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改(通过有无ID区分)")
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "通过ID删除数据")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    @ApiOperation(value = "通过ids批量删除数据")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            searchMasterMsgService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public SearchMasterMsg findOne(@PathVariable("id") Long id) {
        return searchMasterMsgService.loadById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<SearchMasterMsg> findAll() {
        return searchMasterMsgService.loadAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }


    /**
     * 接口：发布寻主消息
     *
     * @param searchMasterMsg
     * @return AjaxResult
     */
    @ApiOperation(value = "发布寻主消息")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            searchMasterMsgService.publish(searchMasterMsg, request);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发布失败!" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，稍后重试!!!");
        }
    }


    // 待审核或驳回：查询待审核或驳回的寻主消息 - 平台管理员才能查询 - 可以通过按钮权限限制，这里不做判断
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        query.setState(0);
        return searchMasterMsgService.queryPage(query);
    }

    // 寻主池：查询审核通过还没有分配店铺或者被拒单的寻主消息 - state=1，ShopId为null
/*@PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery query){
        query.setState(1);
        //由于寻主池查询条件和待处理查询条件冲突，但是查询的结构不一样，所以寻主池业务需要重新写
        //查询state=1，shopId is null的寻主消息
        return searchMasterMsgService.queryPagePool(query);
    }*/
// 已完成：查询已完成的寻主消息 - 平台管理员才能查询 - 可以通过按钮权限限制，这里不做判断
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query) {
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }

    // 用户寻主列表：用于登录之后再个人中心查询自己所有状态的寻主消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> userSearchMasterMsg(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.userSearchMasterMsg(query, request);
    }

    // 待处理：查询审核通过并已经分配了店铺的寻主消息 - state=1，ShopId不为null
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(1);
        return searchMasterMsgService.toHandle(query, request);
    }

    /**
     * 接口：拒单
     *
     * @param id
     */
    @GetMapping("/reject/{id}")
    @ApiOperation(value = "拒单")
    public AjaxResult reject(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("拒单失败");
        }

    }

    /**
     * 接口：发布寻主消息
     *
     * @param msgDto
     * @return AjaxResult
     */
    @ApiOperation(value = "接单")
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody MsgDto msgDto) {
        try {
            searchMasterMsgService.accept(msgDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("接单失败!" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，稍后重试!!!");
        }
    }

}
